package com.lenovo.lcdm.type.controller;

import com.lenovo.lcdm.common.enums.CommonMsgEnum;
import com.lenovo.lcdm.common.enums.ResponseMsgEnum;
import com.lenovo.lcdm.common.model.ResponseVo;
import com.lenovo.lcdm.type.dto.*;
import com.lenovo.lcdm.type.service.TypeDefinitionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @desc 类型定义控制器
 * @author xujing30
 * @date 2024/1/9
 */
@RestController
public class TypeDefinitionController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    TypeDefinitionService typeDefinitionService;


    @GetMapping("/type/root-types")
    public ResponseVo<List<TypeDefinitionDTO>> queryTypes() {
        ResponseVo<List<TypeDefinitionDTO>> response = new ResponseVo<>();
        TypeDefinitionDTO TypeDefinitions = typeDefinitionService.queryRootTypes();
        response.setData(Arrays.asList(TypeDefinitions));
        return response;
    }

    @GetMapping("/type/{id}/detail")
    public ResponseVo<TypeDefinitionDTO> queryDetail(@PathVariable("id") Long id){
        ResponseVo<TypeDefinitionDTO> response = new ResponseVo<>();
        TypeDefinitionDTO data = typeDefinitionService.queryDetail(id);
        response.setData(data);
        return response;
    }

    @GetMapping("/type/{id}/local-attribute")
    public ResponseVo<List<LocalAttributeDTO>> queryAllLocalAttribute(@PathVariable("id") Long id){
        ResponseVo<List<LocalAttributeDTO>> response = new ResponseVo<>();
        List<LocalAttributeDTO> data = typeDefinitionService.queryAllLocalAttribute(id);
        response.setData(data);
        return response;
    }

    @PutMapping("/type/attribute/sort")
    public ResponseVo<TypeDefinitionDTO> sortLocalAttribute(@RequestBody LocalAttributeSortDTO localAttributeSortDTO){
        ResponseVo<TypeDefinitionDTO> response = new ResponseVo<>();
        typeDefinitionService.saveSortAttribute(localAttributeSortDTO);
        TypeDefinitionDTO data = typeDefinitionService.queryDetail(localAttributeSortDTO.getTypeId());
        response.setData(data);
        return response;
    }

    @PostMapping("/type")
    public ResponseVo<Object> saveTypeDefinition(@RequestBody TypeDefinitionDTO typeDefinitionDTO){
        ResponseVo<Object> response = new ResponseVo<>();
        typeDefinitionService.saveTypeDefinition(typeDefinitionDTO);
        response.setCode(ResponseMsgEnum.SUCCESS.getCode());
        return response;
    }


    @PutMapping("/type")
    public ResponseVo<Object> updateTypeDefinition(@RequestBody TypeDefinitionDTO typeDefinitionDTO){
        ResponseVo<Object> response = new ResponseVo<>();
        typeDefinitionService.updateTypeDefinition(typeDefinitionDTO);
        response.setCode(ResponseMsgEnum.SUCCESS.getCode());
        return response;
    }

    @DeleteMapping("/type/{id}")
    public ResponseVo<Object> deleteTypeDefinition(@PathVariable("id") Long id, @RequestParam(required = false, defaultValue = "true") boolean checkSub, @RequestParam(required = false, defaultValue = "true") boolean checkAttribute, @RequestParam(required = false, defaultValue = "false") boolean checkActualValue) {
        ResponseVo<Object> response = new ResponseVo<>();
        typeDefinitionService.deleteTypeDefinition(id, checkSub, checkAttribute, checkActualValue);
        return response;
    }

    @PutMapping("/type/group/sort")
     public ResponseVo sortGroup( @RequestBody GroupSortDTO groupSortDTO){
        ResponseVo response = new ResponseVo<>();
        typeDefinitionService.groupSort(groupSortDTO);
        return response;
    }

    @GetMapping("/type/multiple-level-tree")
    public ResponseVo<TypeDefinitionTreeDTO> getTypeMultipleLevelTree(@RequestParam("name") String typeInternalName) {
        ResponseVo<TypeDefinitionTreeDTO> response = new ResponseVo<>();
        TypeDefinitionTreeDTO typeDefinitionDTO = typeDefinitionService.findTypeMultipleLevelTree(typeInternalName);
        if (typeDefinitionDTO != null) {
            response.setData(typeDefinitionDTO);
        } else {
            response.setMessage("TypeDefinition Object does not exist!");
            response.setCode(CommonMsgEnum.FAIL.getCode());
        }
        return response;
    }


}
